package com.bdqn.petmalldemo.controller;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.bdqn.petmalldemo.bo.OrderAddBo;
import com.bdqn.petmalldemo.common.ResultCodeEnum;
import com.bdqn.petmalldemo.common.ReturnResult;
import com.bdqn.petmalldemo.common.SystemConstants;
import com.bdqn.petmalldemo.common.TokenCache;
import com.bdqn.petmalldemo.entity.Order;
import com.bdqn.petmalldemo.entity.User;
import com.bdqn.petmalldemo.service.OrderService;
import com.bdqn.petmalldemo.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@Api(tags = "订单控制器")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;


    @ApiOperation(value = "生成记录")
    @PostMapping(value = "/add")
    public ReturnResult addOrder(OrderAddBo orderAddBo) {
        ReturnResult returnResult = CommonUtils.checkUserLogin(orderAddBo.getToken());
        if (StringUtils.isEmpty(orderAddBo.getShippingId())) {
            return ReturnResult.error().code(ResultCodeEnum.ARGS_NULL.getCode())
                    .msg(ResultCodeEnum.ARGS_NULL.getMsg());
        }
        orderService.addOrder(orderAddBo);
        return returnResult;
    }

    @ApiOperation(value = "订单列表查询")
    @PostMapping(value = "/search")
    public ReturnResult searchOrder(String token,Integer currentPage,Integer pageSize,Integer role) {
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        User user = JSONObject.parseObject(TokenCache.get(token), User.class);
        //根据不同的角色决定是否传递用户Id做为参数
        Map<String, Object> map = orderService.searchOrder(role.equals(SystemConstants.USER_ROLE.GENERAL)?user.getId():null,
                currentPage, pageSize);
        return returnResult.data(map);
    }

    @ApiOperation(value = "获取订单信息")
    @PostMapping(value = "/get")
    public ReturnResult getOrderInfo(String token,String id){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        Order order = orderService.getById(id);
        return returnResult.data(order);
    }

    @ApiOperation(value = "取消订单")
    @PostMapping(value = "cancel")
    public ReturnResult cancelOrder(String token,String orderId){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        orderService.cancelOrder(orderId);
        return returnResult;
    }


    @ApiOperation(value = "获取订单信息")
    @PostMapping(value = "getStatus")
    public ReturnResult getOrderStatus(String token,String id){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        Order order = orderService.getById(id);
        if (order==null) {
            return  ReturnResult.error().code(ResultCodeEnum.FAILED.getCode())
                    .msg(ResultCodeEnum.FAILED.getMsg());
        }
        return returnResult.data(order.getStatus());
    }

    @ApiOperation(value = "全部发货")
    @PostMapping("sendAll")
    public ReturnResult sendAll(String token,String orderId){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        orderService.sendAll(orderId);
        return returnResult;
    }

    @ApiOperation(value = "确认收货")
    @PostMapping(value = "complete")
    public ReturnResult complete(String token,String orderId){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        orderService.complete(orderId);
        return returnResult;
    }


}

